package com.shine.icis.flow.handler;


import com.shine.cotrun.web.comm.SpringContextLoader;
import com.shine.icis.blo.ISubmitTaskService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.config.SysConstants;
import com.shine.icis.hbean.FlowIns;
import com.shine.icis.hbean.FlowVar;
import com.shine.icis.util.FileHelper;

public class SubmitFlowEndAction implements IFlowHandler {

	@Override
	public void execute(FlowIns fi) throws Exception {
		//移动报送文件目录
//		FlowVar v1 = fi.getFlowVar(SysConstants.FLOW_VAR_SUBMITDIR);
		FlowVar v2 = fi.getFlowVar(SysConstants.FLOW_VAR_SOURCEDIR);
//		FlowVar v3 = fi.getFlowVar(SysConstants.FLOW_VAR_SUBMITDATE);
		FlowVar v4 = fi.getFlowVar(SysConstants.FLOW_VAR_FILENAME);
		FlowVar v5 = fi.getFlowVar(SysConstants.FLOW_VAR_INCREMENT);
		FlowVar v6 = fi.getFlowVar(SysConstants.FLOW_VAR_UNLOADTR12);
		if (v2 == null || v2.getVarValue() == null 
				|| v4 == null || v4.getVarValue() == null
				|| v5 == null || v5.getVarValue() == null
				|| v6 == null || v6.getVarValue() == null) {
			throw new IllegalArgumentException("流程上下文变量为空");
		}
		
		String sourcePath = v2.getVarValue();//生成到临时目录的路径
		String fileName =  v4.getVarValue();//内容如SC_10000000_20100930_001N_@fileCode_@increment.TXT
		boolean increment = Boolean.valueOf(v5.getVarValue());//是否增量
		boolean unloadTr12 = Boolean.valueOf(v6.getVarValue());//是否加载A01－A12
		
		String savePath = SysCodeDictLoader.getInstance().getSubmitConfValue("FILE_SAVE_PATH");//最终生成文件保存目录，有可能是FTP地址（以fpt开头）
		
		boolean isEncrypt = false;
		String pin = null;//加密的PIN码
		String cerPath = null;//公钥路径
		if (!unloadTr12) {//加载A01－A12时判断是否要加密
			String tem = SysCodeDictLoader.getInstance().getSubmitConfValue("FILE_ENCRYPT");
			isEncrypt = tem != null && tem.trim().equals("1");//是否要加密
			if(isEncrypt){  //需要自动加密
				pin = SysCodeDictLoader.getInstance().getSubmitConfValue("ENCRYPT_PIN");
				cerPath = SysCodeDictLoader.getInstance().getSubmitConfValue("ENCRYPT_KEY_CER");
			}
		}
		
		String rdyFilename = SysCodeDictLoader.getInstance().getSubmitConfValue("PROP_RDY_FILENAME");//PROP自动报送的总标志文件名
		
		//报送文件
		FileHelper.submitFile(sourcePath, savePath, pin, cerPath, fileName, increment, isEncrypt, rdyFilename);
		
		ISubmitTaskService srSer = (ISubmitTaskService)SpringContextLoader.getBean("submitTaskService");
		srSer.txArchiveSubmitReport(fi.getInsId());
	}
}
